package com.study.www.config;

import com.study.www.config.protobuf.ProtobufRedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author : Administrator.zhuyanpeng
 * @date : 2019/10/18 - 14:00
 */
@Component
public class RedisUtils {

    @Autowired
    ProtobufRedisTemplate protobufRedisTemplate;

    ValueOperations<String, Object> operations = null;
    ListOperations<String, Object> operationsList = null;
    /*** 默认生存周日 72小时 = 259200S */
    private static final long TWODAY_TIME = 259200L;

    @PostConstruct
    public void init() {
        operations = protobufRedisTemplate.opsForValue();
        operationsList = protobufRedisTemplate.opsForList();
    }

    /**
     * 从缓存中得到数据
     *
     * @param key key
     * @return Object
     */
    public Object get(String key) {
        if (ObjectUtils.isEmpty(key)) {
            return null;
        }
        return operations.get(key);
    }

    /**
     * 设置数据到缓存中
     *
     * @param key   key
     * @param value value
     */
    public void set(String key, Object value) {
        if (!(ObjectUtils.isEmpty(key) || ObjectUtils.isEmpty(value))) {
            operations.set(key, value, TWODAY_TIME, TimeUnit.SECONDS);
        }
    }

    /**
     * 设置数据到缓存中
     *
     * @param key    key
     * @param value  value
     * @param offset 过期时间 S
     */
    public void set(String key, Object value, Long offset) {
        if (!(ObjectUtils.isEmpty(key) || ObjectUtils.isEmpty(value))) {
            offset = Optional.ofNullable(offset).orElseGet(() -> TWODAY_TIME);
            operations.set(key, value, offset, TimeUnit.SECONDS);
        }
    }


    /**
     * 从缓存中删除数据
     *
     * @param key key
     */
    public void delete(String key) {
        if (!ObjectUtils.isEmpty(key)) {
            protobufRedisTemplate.delete(key);
        }
    }


    /**
     * 判断key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public boolean hasKey(String key) {
        if (!ObjectUtils.isEmpty(key)) {
            return protobufRedisTemplate.hasKey(key);
        }
        return false;
    }

    /**
     * 根据key 获取剩余过期时间
     *
     * @param key 键
     * @return 时间(秒) 返回0代表为永久有效
     */
    public long getExpire(String key) {
        if (ObjectUtils.isEmpty(key)) {
            throw new RuntimeException("the redis key is not null!");
        }
        return protobufRedisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    /**
     * 获取list缓存的内容
     *
     * @param key   键
     * @param start 开始
     * @param end   结束  0 到 -1代表所有值
     * @return List list中的所有元素
     */
    public List<Object> lGet(String key, Long start, Long end) {
        if (ObjectUtils.isEmpty(key)) {
            throw new RuntimeException("the redis key is not null!");
        }
        start = Optional.ofNullable(start).orElseGet(() -> 0L);
        end = Optional.ofNullable(end).orElseGet(() -> -1L);
        return operationsList.range(key, start, end);
    }

    /**
     * 设置list 值
     *
     * @param key   键
     * @param value 值
     */
    public void lPush(String key, Object value) {
        operationsList.leftPush(key, value);
    }

    /**
     * 获取list缓存的长度
     * @param key 键
     */
    public long lSize(String key){
        if (ObjectUtils.isEmpty(key)) {
            throw new RuntimeException("the redis key is not null!");
        }
        return operationsList.size(key);
    }
}
